<?php

function smarty_function_assets($params, &$smarty)
{
	$assets = assets::instance( );

	$mode = _::pick( @$params['mode'], $assets->mode(), 'inline' );
	unset( $params['mode'] );
	
	$assign = _::pick( @$params['assign'], null );
	unset( $params['assign'] );
	
	// cleanup and out mode
	if( empty( $params ) )
	{
		$r = $assets ->fetch( );

		// assign?
		if( !is_null( $assign ) )
		{
			$smarty ->assign( $assign, $r );
			$r = '';
		}

		$assets ->printlog( );
		
		return $r;
	}

	$version = isset( $params['version'] ) ? $params['version'] : 0;
	unset( $params['version'] );
	
	$types = explode( ',', 'js,css,set' );
	foreach( $types as $type )
		if( isset( $params[$type] ) )
			$assets ->append( $type, $params[$type], $version );

	// result gen
	switch( $mode )
	{
		case 'normal':
		case 'inhead':
			$r = '';
			break;
		
		default:
		case 'inline':
			$r = $assets ->fetch( );
			break;
	}
	
	// assign?
	if( !is_null( $assign ) )
	{
		$smarty ->assign( $assign, $r );
		$r = '';
	}
	
	return $r;
}

function smarty_function_assets_old($params, &$smarty)
{
	$config = config::instance( );
	$mode = _::pick( @$params['mode'], $config ->assets__mode, 'inline' );
	unset( $params['mode'] );
	
	$assign = _::pick( @$params['assign'], null );
	unset( $params['assign'] );
	
	// cleanup and out mode
	if( empty( $params ) )
	{
		$r = $smarty->__assets['out'];
		$smarty->__assets['out'] = '';
		
		// assign?
		if( !is_null( $assign ) )
		{
			$smarty ->assign( $assign, $r );
			$r = '';
		}
		
		if( !empty( $smarty->__assets['log'] ) )
		{
			bc::info( $smarty->__assets['log'], 'assets' );
			$smarty->__assets['log'] = array( );
		}
		
		return $r;
	}
	
	// cleaning
	foreach( array( 'css', 'js', 'sets' ) as $type )
		if( !isset( $params[$type] ) || !is_string( $params[$type] ) )
			$params[$type] = '';
	
	if( !isset( $smarty->__assets ) || !is_array( $smarty->__assets ) )
		$smarty->__assets = array( 'js' => array( ), 'css' => array( ), 'out' => '' );



	
	// start working
	$version = _::pick( $config ->assets__version, @$params['version'], 0 );
	$_html_result = '';
	
	// parse set of sets
	$assets = $config ->assets__sets;
	$sets = explode( ' ', str_replace( ',', ' ', $params['sets'] ) );
	foreach( $sets as $m )
	{
		$m = trim($m); if( !$m ) continue;
		if( !isset( $assets[$m] ) ) { $smarty->__assets['log'][] = sprintf( 'set %s not found', $m ); continue; }
		$params['js'] = $assets[$m].' '.$params['js']; // only js?
	}
	
	// parse js, css sets
	$js = explode( ' ', str_replace( ',', ' ', $params['js'] ) );
	$css = explode( ' ', str_replace( ',', ' ', $params['css'] ) );
	$css = array_unique( array_merge( $css, $js ) );
	
	
	// assets2html - join!
	foreach( array( 'css', 'js' ) as $type )
	foreach( $$type as $m )
	{
		$m = trim( $m ); if( !$m ) continue;
		$_html_result .= smarty_function_assets__join( $type, $m, $smarty, $version );
	}
	
	
	// result gen
	switch( $mode )
	{
		case 'normal':
		case 'inhead':
		$smarty ->__assets['out'] .= $_html_result;
		$r = '';
		break;
		
		default:
		case 'inline':
		$r = $_html_result;
		$r .= isset($smarty->__assets['out'])?$smarty->__assets['out']:'';
		$smarty->__assets['out'] = '';
		break;
	}
	
	// assign?
	if( !is_null( $assign ) )
	{
		$smarty ->assign( $assign, $r );
		$r = '';
	}
	
	
	return $r;
}

function smarty_function_assets__join( $type, $m, &$smarty, $v = 0 )
{
	$r = '';
	$v = intval( $v );
	
	$patterns = array(
		'js' => '<script src="%1$s" type="text/javascript"></script>',
		'css' => '<link rel="stylesheet" type="text/css" href="%1$s" />',
		);
	
	$pathtpl = 'site/%2$s/%1$s.%2$s';
	$hreftpl = $v ? sprintf('/%%2$s/%%1$s-%d.%%2$s',$v) : '/%2$s/%1$s.%2$s';
	
	// action!
	$nsis = mb_strpos( $m, '::' ) !== false;
	$ns = mb_substr( $m, 0, mb_strpos( $m, '::' ) );// namespaces have own files
	$f = str_replace( '::', '/', $m );
	
	//bc::log( array( $m, $ns, $f ), 'guesses' );
	$files = array( );
	foreach( array( $ns, $f ) as $guess )
	if( $guess )
	{
		$filepath = sprintf( $pathtpl, $guess, $type );
		$filehref = sprintf( $hreftpl, $guess, $type );
		$files[ $filepath ] = array( 'href' => $filehref, 'type' => $type );
	}
	
	foreach( $files as $path => $file )
	if( is_readable( $path ) )
	{
		if( isset( $smarty ->__assets['css'][$path] ) ) continue;
		
		$smarty ->__assets['css'][$path] = true;
		$r .= sprintf( $patterns[$type], $file['href'] )."\n";
	}
	else
		$smarty->__assets['log'][] = sprintf( '%s not found', $path );
	
	return $r;
}

